import os
import re

# log_file_path = 'E:\\Postgraduate\\Robot_Control\\Control_System\\Robot_arm_control_system_9.23\\F407_arm_control_master\\'
log_file_path = os.getcwd()
# file_name = 'Polhemux_data_record'
file_name = 'wrist_stable_data_record'
# file_name = 'wrist_stable_data_record'

# 正则表达式 不能分行处理
# robot_arm_log_get = re.compile('(?P<data_time>.*?)-->.*-->P_sensor1_x:(?P<P_sensor1_x>.*?)cm.*P_sensor1_y:(?P<P_sensor1_y>.*?)cm.*P_sensor1_z:(?P<P_sensor1_z>.*?)cm.*P_sensor1_yaw:(?P<P_sensor1_yaw>.*?)°.*P_sensor1_pitch:(?P<P_sensor1_pitch>.*?)°.*P_sensor1_roll:(?P<P_sensor1_roll>.*?)°.*Joint_M_1(?P<Joint_M_1>.*?)°.*Joint_M_2(?P<Joint_M_2>.*?)°', re.S)
robot_arm_log_get = re.compile('(?P<data_time>.*?)-->.*-->real_r_trachea_1:(?P<real_r_trachea_1>.*?)bar.*real_r_trachea_2:(?P<real_r_trachea_2>.*?)bar.*real_r_trachea_3:(?P<real_r_trachea_3>.*?)bar.*real_r_trachea_4:(?P<real_r_trachea_4>.*?)bar.*real_r_trachea_5:(?P<real_r_trachea_5>.*?)bar.*real_r_trachea_6:(?P<real_r_trachea_6>.*?)bar.*real_r_trachea_7(?P<real_r_trachea_7>.*?)bar.*r_angle_roll:(?P<r_angle_roll>.*?)°.*r_angle_pitch:(?P<r_angle_pitch>.*?)°.*r_angle_yaw:(?P<r_angle_yaw>.*?)°.*target_roll:(?P<target_roll>.*?)°', re.S)



def deal_log_to_csv(rule):

    log_file = log_file_path + '\\' + file_name + '.log'
    csv_file = log_file_path + '\\' + file_name + '.csv'

    file_storage_path = log_file_path
    if not os.path.exists(file_storage_path):
        os.makedirs(file_storage_path)


    with open(log_file, 'r', encoding='utf-8') as f:
        # open source file
        file_content_lines = f.readlines() #全部读成一行 1 x n的矩阵

    # 检查文件并清除数据
    if os.path.exists(csv_file):
        # 检查文件是否有数据（即文件大小大于0）
        if os.path.getsize(csv_file) > 0:
            # 打开文件并清空内容
            open(csv_file, 'w').close()

    # write file head line
    with open(csv_file, 'a', encoding='utf-8') as f:
        f.write('data_time' + ',' + 'real_r_trachea_1' + ', ' + 'real_r_trachea_2' + ',' + 'real_r_trachea_3' + ',' +
                'real_r_trachea_4' + ',' + 'real_r_trachea_5' + ',' + 'real_r_trachea_6' + ',' + 'real_r_trachea_7' + ',' +
                'r_angle_roll' + ',' + 'r_angle_pitch' + ',' + 'r_angle_yaw' + ',' + 'target_roll' + '\n')

    # # write file real data
    k = 0
    for file_content_line in file_content_lines:
        k = k + 1
        print(str(k) + '\n')
        file_re_data = rule.finditer(file_content_line)
        for it in file_re_data:
            with open(csv_file, 'a', encoding='utf-8') as f:
                f.write(it.group('data_time') + ',' + it.group('real_r_trachea_1') + ',' + it.group('real_r_trachea_2')
                        + ',' + it.group('real_r_trachea_3') + ',' + it.group('real_r_trachea_4') + ','
                        + it.group('real_r_trachea_5') + ',' + it.group('real_r_trachea_6') + ','
                        + it.group('real_r_trachea_7') + ',' + it.group('r_angle_roll') + ',' + it.group('r_angle_pitch')
                        + ',' + it.group('r_angle_yaw') + ',' + it.group('target_roll') + '\n')
            print("wait.......")

    print("File Deal finish!")
    return file_storage_path

if __name__=='__main__':
    deal_log_to_csv(robot_arm_log_get)
    # print(os.getcwd())  # 获取当前工作目录路径
    # print(os.path.abspath('.'))  # 获取当前文件目录路径
    # print(os.path.abspath('Deal_log_to_csv.py'))  # 获取当前目录文件下的文件目录路径
    # print(os.path.abspath('..'))  # 获取当前文件目录的父目录 ！注意是父目录路径
    # print(os.path.abspath(os.curdir))  # 获取当前文件目录路径
    #
    # print(log_file_path)



